define(function(require, exports){
	var pubjs = require('pubjs');
	var $ = require('jquery');
	var util = require('util');
	// var baseGrid	= require('@base/grid').baseNoDate;
	var grid  = require('@base/highGrid');
	var labels = require('@sys_labels').labels;

	var needSetSpot = [];

	var Base = grid.base.extend({
		init: function(config){
			config = pubjs.conf(config, {
				'cols': [
					{name:"_id", text: 'ID', width: 50, type:'fixed',sort:true,align:'center'},
					{name: 'Name', text: LANG('订单名称'), render:'renderName',width:200},
					{name: 'Time', text: LANG('订单时间'), type:'index',render:'renderDealTime',width:200},
					{name: 'Price', text: LANG('订单价格'), type:'index',render:'renderPrice',width:200}
				],
				metrics: [
					{name: 'Status', text:LANG('状态'), render:'renderStatus', align:'center'},
					{name: 'channelName', text:LANG('渠道'), align:'center'},
					{name: 'SpotsInfo', text:LANG('广告位数量'), render:'renderCount',align:'center'},
					{name: 'UpdateTime', text:LANG('更新时间'), render: 'renderTime',align:'center'}
				],
				'param': {
					order: 'CreateTime|-1'
				},
				'hasMenu': true,
				'hasTab': false,
				'hasExport': false,
				'hasAmount': false,
				'hasSearch': false,
				'hasSwitch': false,
				'hasSubGrid': false,
				'url': 'deal/list',
				'reqType': 'websocket',
				'gridName': 'deal'
			});

			this.$channels = pubjs.config('channelMap');

			this.Super('init', arguments);
		}
		,renderPrice: function(i, val, data) {
			var price = data.DealInfo && data.DealInfo.Price || 0;
			return labels.formatCurrencyKeep2(price);
		}
		,renderDealTime: function(i, val, data) {
			var info;
			var ret = '';
			var begin, end, beginKey;

			// 需要手动设置订单开启时间
			if(~needSetSpot.indexOf(data.AdxId))
			{
				info = data && data.ManualInfo;
				beginKey = 'StartTime';
			}
			// 渠道同步过来的开启结束时间
			else
			{
				info = data && data.DealInfo;
				beginKey = 'BeginTime';
			}

			if(info)
			{
				begin = info[beginKey] ? util.date("Y-m-d", info[beginKey]) : null;
				end = info.EndTime ? util.date("Y-m-d", info.EndTime) : null;
			}

			ret += begin ? begin : LANG('未知');
			ret += ' - ' + (end ? end : LANG('不限'));
			return !begin && !end ? LANG('不限') : ret;

		}
		,renderTime: function(i, val) {
			return val ? util.date("Y-m-d H:i:s", val) : null;
		}
		,renderStatus: function(i,val,data,con){
			var text;
			switch (+val) {
				case 1:
					text = LANG('开启');
					break;
				case 2:
					text = LANG('关闭');
					break;
				default :
					text = '-';
			}
			return text;
		}
		,renderName: function(i, val, data, con){
			val = val || LANG('-');
			return $('<a class="M-tableListWidthLimit"/>')
				.attr({
					'title': val,
					'target': '_blank',
					'href': '#deal/edit/'+ data._id + '?Type=' + this.parent().getConfig('Type')
				})
				.text(val).width(con.width);
		}
		,renderCount: function(idx, val, row, col){
			return (val && val.length || 0).toString();
		}
	});

	exports.base = Base

});